package cn.jiyun.controller;

import cn.jiyun.service.IFavoriteService;
import cn.jiyun.model.pojo.Favorite;
import cn.jiyun.model.query.FavoriteQuery;
import cn.jiyun.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【-Favorite】操作接口
 *
 * @author
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/api/admin/favorite")
@Api(tags = "操作接口")
public class FavoriteController {

    @Autowired
    public IFavoriteService favoriteService;

    /**
     * 接口：添加
     * @param favorite 要添加的
     * @return Result 响应给前端
     */
    @PostMapping
    @ApiOperation("添加" )
    public Result save(@RequestBody Favorite favorite) {
        favoriteService.save(favorite);
        return Result.ok();
    }

    /**
     * 接口：根据Id删除
     * @param id 的Id
     * @return Result 响应给前端
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("根据Id删除")
    public Result remove(@PathVariable("id" ) Long id) {
        favoriteService.removeById(id);
        return Result.ok();
    }

    /**
     * 接口：修改
     * @param favorite 要修改的数据
     * @return Result 响应给前端
     */
    @PutMapping("update")
    @ApiOperation("修改")
    public Result updateById(@RequestBody Favorite favorite) {
        favoriteService.updateById(favorite);
        return Result.ok();
    }


    /**
      * 根据Id查询单个
     */
    @GetMapping("/get/{id}")
    @ApiOperation("根据Id查询单个")
    public Result getById(@PathVariable Long id)  {
        Favorite one = favoriteService.getById(id);
        return Result.ok(one);
    }

    /**
     * 接口：查询所有
     */
    @GetMapping
    @ApiOperation("查询所有")
    public Result getList() {
        List<Favorite> listAll = favoriteService.list();
        return Result.ok(listAll);
    }


    /**
     * 接口：分页查询所有
     * @return List<Favorite> 所有数据
     */
    @GetMapping("/{page}/{limit}")
    @ApiOperation("获取所有")
    public Result getPageList(@PathVariable Integer page, @PathVariable Integer limit, FavoriteQuery query) {
        //封装分页参数
        query.setCurrentPage(page);
        query.setPageSize(limit);
        //查询分页数据
        IPage<Favorite> pageList = favoriteService.getPageList(query);
        return Result.ok(pageList);
    }

}
